package zhang.lao.api.testThread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import zhang.lao.api.controller.TestAotuController;
import zhang.lao.build.mybatis.jdbc.auto.model.WnLnglatAutomatic;
import zhang.lao.build.mybatis.jdbc.auto.model.WnLnglatAutomaticNew;
import zhang.lao.build.mybatis.jdbc.auto.model.WnLnglatAutomaticNewExample;
import zhang.lao.build.tool.CoordinateConversion;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.Point;
import zhang.lao.dao.base.WnLnglatAutomaticNewDao;
import zhang.lao.extents.spring.SpringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2018-06-04 16:52
 **/
public class UpLoadLatLng implements Runnable {
    private WnLnglatAutomaticNewDao wnLnglatAutomaticNewDao = SpringUtils.getBean(WnLnglatAutomaticNewDao.class);
    private JSONArray jsonArray;
    private int deviceId;

    public int getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(int deviceId) {
        this.deviceId = deviceId;
    }

    public JSONArray getJsonArray() {
        return jsonArray;
    }

    public UpLoadLatLng(JSONArray jsonArray, int deviceId) {
        this.jsonArray = jsonArray;
        this.deviceId = deviceId;
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }

    @Override
    public void run() {
        TestAotuController.machineMap.put(deviceId+"",true);
        for (Object coordinate : jsonArray) {
            if(TestAotuController.canRun) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                WnLnglatAutomaticNewExample wnLnglatAutomaticNewExample = new WnLnglatAutomaticNewExample();
                wnLnglatAutomaticNewExample.createCriteria().andDeviceIdEqualTo(deviceId);
                WnLnglatAutomaticNew first = ListUtils.getFirst(wnLnglatAutomaticNewDao.selectByExample(wnLnglatAutomaticNewExample));
                JSONObject coordinate1 = (JSONObject) coordinate;
                Point point = new Point();
                // CoordinateConversion.wgs_gcj_encrypts(coordinate1.getDouble("lat"), coordinate1.getDouble("lon"))
                point.setLng( coordinate1.getDouble("lon"));
                point.setLat( coordinate1.getDouble("lat"));
                if (first != null) {

                    first.setLat(point.getLat() + "");
                    first.setLng(point.getLng() + "");
                    first.setCreateTime(new Date());
                    wnLnglatAutomaticNewDao.updateByPrimaryKey(first);
                } else {
                    first = new WnLnglatAutomaticNew();
                    first.setCreateTime(new Date());
                    first.setDeviceId(deviceId);
                    first.setDeviceSign(deviceId + "");
                    first.setLat(point.getLat() + "");
                    first.setLng(point.getLng() + "");
                    first.setOrentation(90);
                    first.setHeight(1.0);
                    first.setAbLineId(1);
                    first.setControlType("1");
                    first.setWorkWidth(7.0);
                    first.setSpeed(20.0);
                    wnLnglatAutomaticNewDao.insertSelective(first);
                }
            }else{
                break;
            }
        }
        TestAotuController.machineMap.put(deviceId+"",false);
    }
}
